<?php

	//返回值为应用ID数组
	function get_page_apps($tid,$content,$DB) {
		//分析软件列表
		$content = explode('<ul class="list_6 mt10" id="tab_1229_4">', $content);
		$content = explode('<div class="page_bar">',$content[1]);
		$content = $content[0];
// 		echo $content[0];exit;
		$match_str = '/<li><a href=\"(.*)\" title=\"(.*)\"><img src=\"(.*)\" class=\"icon\" alt=\"\"\/><\/a><a href=\'(.*)\' target=\"_blank\" class=\"title\">(.*)<span class=\"version\">(.*)<\/span><\/span><\/a><div class=\"article_infor\"><span class=\"any\">下载：<span class=\"a_i_c\">(.*)<\/span><\/span><span class=\"size\">大小：<span class=\"a_i_c\">(.*)<\/span><\/span><span class=\"date\">发布日期：<span class=\"a_i_c\">(.*)<\/span><\/span><\/div><div class=\"article\">(.*)<\/div><\/li>/isU';
		preg_match_all($match_str,$content,$urln);
		/**
		 * 0 软件ID
		 * 1 软件信息相对地址
		 * 2 应用名称
		 * 3 原始LOGO图片链接
		 * 4 软件信息相对地址
		 * 5 应用名称
		 * 6 版本号
		 * 7 下载次数
		 * 8 大小
		 * 9 发布日期
		 * 10 简介
		 */
		//unset($urln[0]);
		unset($urln[4]);
		unset($urln[5]);
		//单独取出软件ID并入库
		$ids = array();
		foreach ($urln[1] as $k => $v) {
			$id = explode('pp', $v);
			$id = explode('.html', $id[1]);
			$urln[0][$k] = $id[0];
			$ids[] = $id[0];
			//检查是否存在
			$where = 'AND id= ' . $id[0] .'';
			$result = $DB->build_and_exec_query(
					array('select' => 'COUNT(*) as max','from'   => 'gfunapps','where'	=> "1 " . $where ,)
			);
			//当已经存在的时候跳过插入
			if ($result['max'] > 0) {
				continue;
			}
			//入库操作(注意是否需要对软件名称以及描述编码)
			do_appinsert($urln[0][$k],$tid, $urln[2][$k], $urln[6][$k], $urln[8][$k], basename($urln[3][$k]), $urln[3][$k], 'null', strtotime(trim($urln[9][$k])), $urln[7][$k], 'null', $urln[10][$k]);
			
		}
		unset($urln);
		unset($content);
		return $ids;
	}
	
	
	function get_single_app($DB,$gfun_app_pre,$appid) {
		$content = file_get_contents(make_app_url($gfun_app_pre, $appid));
		//名字,临时用的
		$name = explode('<div class="app_name">', $content);
		$name = explode('<span',$name[1]);
		$name = trim($name[0]);
		//作者
		$author = explode('作者：<span class="d_i_c">', $content);
		$author = explode('</span>',$author[1]);
		$author = trim($author[0]);
		//支持OS
		$s_os = explode('支持OS：<span class="d_i_c">', $content);
		$s_os = explode('</span>',$s_os[1]);
		$s_os = trim($s_os[0]);
		//下载链接
		$d_url = explode('"二维码"', $content);
		$d_url = explode('</span>',$d_url[1]);
		$d_url = explode('src="',$d_url[0]);
		$d_url = explode('"',$d_url[1]);
		$d_url = trim($d_url[0]);
		$array_file = array('down','Pfiles','apk');
		$array_img = array('image','PBarcode','png');
		$d_url = str_replace($array_img, $array_file, $d_url);
		//图片链接
		$i_url = explode('<ul class="clearfix" id="shot0106">', $content);
		$i_url = explode('</ul>',$i_url[1]);
		$match_str = '/src=\"(.*)\" class/isU';
		preg_match_all($match_str,$i_url[0],$i_urln);
		$i_url = $i_urln[1];
		
		
		
		unset($i_urln);
		unset($content);
		//------------------------------更新软件信息------------------------------
		$where = 'AND id= ' . $appid .'';
		$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'gfunapps','where'	=> "1 " . $where ,)
		);
		//当已经存在的时候更新
		if ($result['max'] > 0) {
			$DB->do_update('gfunapps', array(
					'author'					=> $author,
					'supportedos'				=> $s_os,
					),
				'id=' . $appid . ''
			);
		}
		
		//-----------------------下载表插入------------------
		$where = 'AND appid= ' . $appid .'';
		$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'gfunappds','where'	=> "1 " . $where ,)
		);
		//当已经存在的时候更新
		if ($result['max'] < 1) {
			$DB->do_insert('gfunappds', array(
					'appid'					=> $appid,
					'downorgurl'			=> $d_url,
					'downurl'				=> basename($d_url),
				)
			);
		}
		
		//-------------------------图片插入--------------------------
		$DB->simple_delete('gfunpics' , ' appid= ' . $appid .'');
		$DB->query($DB->cur_query);
		$DB->cur_query = '';
		foreach ($i_url as $v) {
			if ($v == 'Images/default_icon.png'){
				$v = 'http://apk.gfan.com/Images/default_icon.png';
			}
			//插入前先删除
			$DB->do_insert('gfunpics', array(
					'appid'					=> $appid,
					'imgorgurl'				=> $v,
					'imgurl'				=> basename($v),
				)
			);
		}
		echo '软件<B><font color="red">' . $name . '</font></B>采集完毕<BR>' ;
	}
	
	function make_app_url($gfun_app_pre,$appid){
		return $gfun_app_pre . 'App' . $appid . '.html';
	}
	
	
	function do_appinsert($id,$tid,$name,$ver,$size,$logo,$orglogo,$author,$lastupdate,$downloadcount,$supportedos,$des){
		global $DB;
		$result = $DB->do_insert( 'gfunapps', array(
				'id'					=> $id,
				'tid'					=> $tid,
				'name'					=> $name,
				'ver'					=> $ver,
				'size'					=> $size,
				'logo'					=> $logo,
				'orglogo'				=> $orglogo,
				'author'				=> $author,
				'lastupdate'			=> $lastupdate,
				'downloadcount'			=> $downloadcount,
				'supportedos'			=> $supportedos,
				'des'					=> $des,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
	
	
	
?>